// RefundRecordDao.java
package com.dw.dao.fee;

import com.dw.model.fee.RefundRecord;
import com.dw.util.DBUtil;

import java.math.BigDecimal;
import java.sql.*;

public class RefundRecordDao {
    public void create(Connection conn, RefundRecord record) throws SQLException {
        String sql = "INSERT INTO refund_record (charge_no, refund_amount, operator_id, reason) "
                + "VALUES (?,?,?,?)";

        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, record.getChargeNo());
            pstmt.setBigDecimal(2, record.getRefundAmount());
            pstmt.setInt(3, record.getOperatorId());
            pstmt.setString(4, record.getReason());
            pstmt.executeUpdate();
        }
    }

    public BigDecimal getDailyRefundTotal() throws SQLException {
        String sql = "SELECT SUM(refund_amount) AS total_refund "
                + "FROM refund_record "
                + "WHERE DATE(refund_time) = CURDATE()";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            if (rs.next()) {
                BigDecimal total = rs.getBigDecimal("total_refund");
                return total != null ? total : BigDecimal.ZERO;
            }
            return BigDecimal.ZERO;
        } catch (SQLException e) {
            throw new SQLException("获取当日退费总额失败: " + e.getMessage(), e);
        }
    }
}